package com.itheima.consumer.listener;

import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class MessageListener {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue", durable = "true"),
            exchange = @Exchange(name = "delay.direct", delayed = "true"),
            key = "delay"
    ))
    public void listenDelayMessage(String msg){
        log.info("接收到delay.queue的延迟消息：{}", msg);
    }




    @RabbitListener(queues = "dlx.queue")
    public void listenerDlxQueue(String msg){
        log.info("msg:{}",msg);
    }


    @RabbitListener(queues = "object.queue")
    public void objectListener1(org.springframework.amqp.core.Message msg){
        log.info("objectListener1-收到的消息是:{}", new String(msg.getBody()));
        log.info("消息ID是:{}", msg.getMessageProperties().getMessageId());
    }

//    @RabbitListener(queues = "object.queue")
//    public void objectListener1(String msg){
//        log.info("objectListener1-收到的消息是:{}",msg);
//    }



    //交换机
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = "hmall.direct.queue1"),
                    exchange = @Exchange(value = "hmall.direct.exchange1",type = ExchangeTypes.DIRECT),
                    key = {"java","php"}
            )
    })
    public void annotationListener(String msg){
        log.info("message:{}",msg);

    }


    @RabbitListener(queues = "topic.queue1")
    public void topicQueueListener1(String msg){
        log.info("topicQueueListener1-收到的消息是:{}",msg);
    }

    @RabbitListener(queues = "topic.queue2")
    public void topicQueueListener2(String msg){
        log.info("topicQueueListener2-收到的消息是:{}",msg);
    }




    @RabbitListener(queues = "direct.queue1")
    public void directQueueListener1(String msg){
        log.info("directQueueListener1-收到的消息是:{}",msg);
    }

    @RabbitListener(queues = "direct.queue2")
    public void directQueueListener2(String msg){
        log.info("directQueueListener2-收到的消息是:{}",msg);
    }


    @RabbitListener(queues = "fanout.queu1")
    public void listenFanoutQueue1(String msg) {
        System.out.println("消费者1接收到Fanout消息：【" + msg + "】");
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg) {
        System.out.println("消费者2接收到Fanout消息：【" + msg + "】");
    }


    @RabbitListener(queues = "work.queue")
    public void workQueueListener1(String msg){
        log.info("workQueueListener1收到的消息是:{}",msg);
    }

    @RabbitListener(queues = "work.queue")
    public void workQueueListener2(String msg){
        log.info("workQueueListener2收到的消息是:{}",msg);
    }

    @RabbitListener(queues = "simple.queue")
    public void simpleListener(String msg){
        log.info("收到的消息是:{}",msg);
    }
}
